package demoServlet01;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/***
 * Http 服务器和servlet的关系
 * @author zangyiwei 
 * @email zanglaowei@163.com
 * @date 2020年4月7日
 */
public class HelloServlet extends HttpServlet { 
	private static final long serialVersionUID = 1L; 
	private String msg = "Hello World!"; 
	public HelloServlet() { } 
	public HelloServlet(String msg) { 
		this.msg = msg; 
	} 
//	?name=123
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		response.setContentType("text/html");  
		response.setStatus(HttpServletResponse.SC_OK);  
		String strName=request.getParameter("name");
		response.getWriter().println("<h1>" + msg + "</h1><h2>"+ strName +"<h2>");  
		response.getWriter().println("session=" + request.getSession(true).getId());
	}
	
	 @Override    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {    
       
        String strName=request.getParameter("name");
        System.out.println("Post data:"+strName);
        
        response.setContentType("text/html");  
		response.setStatus(HttpServletResponse.SC_OK);  
		response.getWriter().println("<h1> Post数据" + strName + "</h1>");     
    }  
}